package com.dingqb.mycs.ui.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.yunzhisheng.cn.oraleval.demo.example.NewDemoActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.base.BaseActivity;
import com.dingqb.mycs.AutoNewLineActivity;
import com.dingqb.mycs.DataBaseActivity;
import com.dingqb.mycs.HandlerActivity;
import com.dingqb.mycs.MyViewActivity;
import com.dingqb.mycs.R;
import com.dingqb.mycs.ui.entity.PersonEntity;
import com.dingqb.mycs.ui.nativecode.UrlConnectionActivity;
import com.dingqb.mycs.ui.res.AssetRawActivity;
import com.dingqb.mycs.ui.res.MiMeActivityctivity;
import com.dingqb.util.DensityUtil;
import com.dingqb.util.ScreenWH;
import com.dingqb.util.SystemUtils;
import com.google.gson.Gson;

/**
 * 
 * @author DingQingbo
 *
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("InlinedApi")
public class MainTestActivity extends BaseActivity implements OnClickListener {
	private RequestQueue mQueue;
	private String mUrl = "http://192.168.10.68:8080/MyFirstCS/TestServlet";
	private String mUrl2 = "http://192.168.10.68:8080/MyFirstCS/LoginServlet";
	private TextView tv_1;
	private TextView tv_login;
	private Button btn_voice;
	private TextView tv_size;
	private TextView tv_html_show;
	private TextView tv_asset_raw;
	private TextView tv_mime;
	private TextView tv_okhttp;
	private TextView tv_url;
	private TextView tv_vollery;
	private TextView tv_myview;
	private TextView tv_auto_newline;
	private TextView tv_handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_test);
		mQueue = Volley.newRequestQueue(this);
		initView();
		Rect rectangle = new Rect();
		Window window = getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(rectangle);

		int statusBarHeight = rectangle.top;
		int statusBarBottom = rectangle.bottom;
		int statusBarLeft = rectangle.left;
		int statusBarRight = rectangle.right;
		int tt = getStatusBarHeight(this);

		fullScreen(window, tv_1);
		int dpi = ScreenWH.getScreenDPI(this);
		int h = ScreenWH.getScreenHeight(this);
		int h2 = ScreenWH.getScreenHeight2(this);
		int h3 = ScreenWH.getScreenHeight3(this);
		int w = ScreenWH.getScreenWidth(this);
		int w2 = ScreenWH.getScreenWidth2(this);
		int w3 = ScreenWH.getScreenWidth3(this);
		float density = ScreenWH.getScreenDensity(this);
		int s = 0;
		tv_size.setText("DPI=" + dpi + ",density=" + density + "\n" + SystemUtils.getMac(this));

	}

	private void fullScreen(Window window, View view) {
		// 透明状态栏
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
		// 透明状态栏
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		// 透明导航栏
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

		view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
	}

	private void initView() {
		tv_1 = (TextView) findViewById(R.id.tv_1);
		tv_1.setOnClickListener(this);
		tv_login = (TextView) findViewById(R.id.tv_login);
		tv_login.setOnClickListener(this);
		btn_voice = (Button) findViewById(R.id.btn_voice);
		btn_voice.setOnClickListener(this);
		tv_size = (TextView) findViewById(R.id.tv_size);
		tv_html_show = (TextView) findViewById(R.id.tv_html_show);
		tv_html_show.setOnClickListener(this);
		tv_size.setOnClickListener(this);
		tv_asset_raw = (TextView) findViewById(R.id.tv_asset_raw);
		tv_asset_raw.setOnClickListener(this);
		tv_mime = (TextView) findViewById(R.id.tv_mime);
		tv_mime.setOnClickListener(this);
		tv_okhttp = (TextView) findViewById(R.id.tv_okhttp);
		tv_okhttp.setOnClickListener(this);
		tv_url = (TextView) findViewById(R.id.tv_url);
		tv_vollery = (TextView) findViewById(R.id.tv_vollery);
		tv_myview = (TextView) findViewById(R.id.tv_myview);
		tv_auto_newline = (TextView) findViewById(R.id.tv_auto_newline);
		tv_handler = (TextView) findViewById(R.id.tv_handler);
		tv_handler.setOnClickListener(this);
		tv_auto_newline.setOnClickListener(this);
		tv_myview.setOnClickListener(this);
		tv_vollery.setOnClickListener(this);
		tv_url.setOnClickListener(this);

	}

	/**
	 * 获取状态栏高度
	 * 
	 * @param context
	 * @return 像素
	 */
	public int getStatusBarHeight(Context context) {
		int result = 0;
		int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
		if (resourceId > 0) {
			result = context.getResources().getDimensionPixelSize(resourceId);
		}
		return result;
	}

	@Override
	protected void onResume() {
		super.onResume();
		// myHttp();
	}

	

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.tv_login) {
			postd();
		} else if (id == R.id.btn_voice) {
			// String s=null;
			// System.out.println(s.length());
			Intent intent = new Intent(MainTestActivity.this, NewDemoActivity.class);
			startActivity(intent);
		} else if (id == R.id.tv_1) {
			int hh = tv_size.getHeight();
			int ww = tv_size.getWidth();
			Toast.makeText(MainTestActivity.this,
					"hh==" + DensityUtil.px2dip(MainTestActivity.this, hh) + ",\nww=" + DensityUtil.px2dip(MainTestActivity.this, ww), 1).show();
			tv_1.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
		} else if (R.id.tv_html_show == id) {
			Intent intent = new Intent(MainTestActivity.this, HtmlActivity.class);
			startActivity(intent);
		} else if (id == R.id.tv_asset_raw) {
			Intent intent = new Intent(this, AssetRawActivity.class);
			startActivity(intent);
		} else if (id == R.id.tv_mime) {
			startActivity(new Intent(MainTestActivity.this, MiMeActivityctivity.class));

		} else if (id == R.id.tv_okhttp) {
			startActivity(new Intent(MainTestActivity.this, OkHttpActivity.class));
		} else if (R.id.tv_url == id) {
			Intent intent = new Intent(MainTestActivity.this, UrlConnectionActivity.class);
			startActivity(intent);
		} else if (id == R.id.tv_vollery) {
			startActivity(new Intent(MainTestActivity.this, VolleryActivity.class));

		} else if (id == R.id.tv_myview) {
			startActivity(new Intent(MainTestActivity.this, MyViewActivity.class));
		}else if(id==R.id.tv_auto_newline){
			startActivity(new Intent(MainTestActivity.this, AutoNewLineActivity.class));
		}else if(R.id.tv_handler==id){
			startActivity(new Intent(MainTestActivity.this, HandlerActivity.class));
		}
		

	}

	/**
	 * xml 文件中的点击事件
	 * @param view
	 */
	public void sqlite(View view){
		startActivity(new Intent(MainTestActivity.this, DataBaseActivity.class));
		
	}
	@Override
	protected void networkChanged(boolean available, boolean isWifi) {
		super.networkChanged(available, isWifi);
	}
	
	
	/**
	 * 
	 */
	public void myHttp() {
		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(mUrl, null, new Response.Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				String result = response.toString();
				if (result != null) {
					Gson gson = new Gson();
					PersonEntity entity = gson.fromJson(result, PersonEntity.class);
					Toast.makeText(MainTestActivity.this, "结果" + entity.getAge(), Toast.LENGTH_SHORT).show();
					tv_1.setText("年纪==" + entity.getAge());
				} else {
					Toast.makeText(MainTestActivity.this, "结果 null", Toast.LENGTH_SHORT).show();
				}

			}
		}, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError arg0) {
				Toast.makeText(MainTestActivity.this, "服务器出错了", Toast.LENGTH_SHORT).show();

			}

		});
		mQueue.add(jsonObjectRequest);
	}

	/**
 * 
 */
	public void postd() {
		RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

		StringRequest stringRequest = new StringRequest(Request.Method.POST, mUrl2, new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(response);
					String result = jsonObject.getString("result");
					Toast.makeText(MainTestActivity.this, result, Toast.LENGTH_LONG).show();
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				Toast.makeText(MainTestActivity.this, "服务器出错了", Toast.LENGTH_SHORT).show();

			}
		}) {
			@Override
			protected Map<String, String> getParams() {
				// 在这里设置需要post的参数
				Map<String, String> map = new HashMap<String, String>();
				map.put("name", "tom");
				map.put("password", "123");
				return map;
			}
		};

		requestQueue.add(stringRequest);

	}

//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			ToQuitTheApp();
//			return false;
//		}
//		return super.onKeyDown(keyCode, event);
//	}
//
//	private boolean isExit = false;
//
//	/**
//	 * 双击back 2次退出程序
//	 */
//	private void ToQuitTheApp() {
//		if (isExit) {
//			// ACTION_MAIN with category CATEGORY_HOME 启动主屏幕
//			Intent intent = new Intent(Intent.ACTION_MAIN);
//			intent.addCategory(Intent.CATEGORY_HOME);
//			startActivity(intent);
//			System.exit(0);// 使虚拟机停止运行并退出程序
//		} else {
//			isExit = true;
//			Toast.makeText(MainTestActivity.this, "再按一次退出APP", Toast.LENGTH_SHORT).show();
//			mHandler.sendEmptyMessageDelayed(0, 3000);// 3秒后发送消息
//		}
//	}
//
//	// 创建Handler对象，用来处理消息
//	Handler mHandler = new Handler() {
//
//		@Override
//		public void handleMessage(Message msg) {// 处理消息
//			super.handleMessage(msg);
//			isExit = false;
//		}
//	};

}
